#include "widget.h"
#include "ui_widget.h"
#include<QPushButton>
#include<QDebug>
#include <QObject>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,target(new QPushButton("target",this))
{
    ui->setupUi(this);

    setPosition();

    // QPushButton* button=new QPushButton("按钮",this);
    // button->move(200,300);//位置设置x,y
    // QRect rect= button->geometry();
    // qDebug()<<"rect.x: "<<rect.x()<<"rect y: "<<rect.y()<<"rect width: "<<rect.width()<<"rect height: "<<rect.height();
    // rect.setX(300),rect.setY(200),rect.setWidth(200),rect.setHeight(60);
    // button->setGeometry(rect);
    // qDebug()<<"rect.x: "<<rect.x()<<"rect y: "<<rect.y()<<"rect width: "<<rect.width()<<"rect height: "<<rect.height();
    // button->setGeometry(300,200,150,45);//推荐
    // //qDebug()<<"rect.x: "<<rect.x()<<"rect y: "<<rect.y()<<"rect width: "<<rect.width()<<"rect height: "<<rect.height();
    // qDebug()<<button->geometry();

    //connect(up,&QPushButton::clicked,target,&Widget::clickUp);


}

Widget::~Widget()
{
    delete ui;
}

void Widget::setPosition()
{

    QPushButton* up=new QPushButton("up",this);
    QPushButton* down=new QPushButton("down",this);
    QPushButton* left=new QPushButton("left",this);
    QPushButton* right=new QPushButton("right",this);
    connect(up,&QPushButton::clicked,this,&Widget::clickUp);//要与信号的参数匹配（this?）
    connect(down,&QPushButton::clicked,this,&Widget::clickdown);//要与信号的参数匹配
    connect(left,&QPushButton::clicked,this,&Widget::clickleft);//要与信号的参数匹配
    connect(right,&QPushButton::clicked,this,&Widget::clickright);//要与信号的参数匹配

    this->target->move(200,200);
    up->move(200,400);
    down->move(200,430);
    left->move(150,210);
    right->move(250,210);



}

void Widget::clickUp()
{
    QRect rect=this->target->geometry();
    this->target->setGeometry(rect.x(),rect.y()-10,rect.width(),rect.height());
}

void Widget::clickdown()
{
    QRect rect=this->target->geometry();
    this->target->setGeometry(rect.x(),rect.y()+10,rect.width(),rect.height());
}

void Widget::clickleft()
{
    QRect rect=this->target->geometry();
    this->target->setGeometry(rect.x()-10,rect.y(),rect.width(),rect.height());
}

void Widget::clickright()
{
    QRect rect=this->target->geometry();
    this->target->setGeometry(rect.x()+10,rect.y(),rect.width(),rect.height());
}
